package back

import (
	"fmt"
	"math"
	"net/http"
	"os"
	"path"
	"strconv"
	"time"
	"tmall_go/models"

	"github.com/gin-gonic/gin"
)

type GoodsController struct{}

func (con GoodsController) PageList(c *gin.Context) {
	// 验证用户是否过期
	_, err := c.Cookie("id")
	if err != nil {
		c.Redirect(http.StatusFound, "/back/login?error=3")
		return
	}
	resStrTag := c.Query("res")
	userName, _ := c.Cookie("name")
	authority, _ := c.Cookie("authority")

	numStr := c.Param("num")
	curPage, _ := strconv.Atoi(numStr)
	curTiao := 0
	var sumPage int64 = 0
	var sumTiao int64 = 0
	pageSize := 5
	dbList := []models.Goods{}
	allList := []models.Goods{}
	models.DB.Order("update_time desc").Offset((curPage - 1) * pageSize).Limit(pageSize).Find(&dbList)
	models.DB.Find(&allList).Count(&sumTiao)
	sumPage = int64(math.Ceil(float64(sumTiao) / float64(pageSize)))
	curTiao = len(dbList)
	c.HTML(http.StatusOK, "backstage/index.html", gin.H{
		"barKey":    3,
		"userName":  userName,
		"authority": authority,
		"errTag":    resStrTag,
		"info":      dbList,
		"curPage":   curPage,
		"curTiao":   curTiao,
		"sumPage":   sumPage,
		"sumTiao":   sumTiao,
		"pageSize":  pageSize,
	})
}
func (con GoodsController) DoGoods(c *gin.Context) {
	// 验证用户是否过期
	_, err := c.Cookie("id")
	if err != nil {
		c.Redirect(http.StatusFound, "/back/login?error=3")
		return
	}
	id := c.Param("id")
	title := c.PostForm("title")
	category := c.PostForm("category")
	price := c.PostForm("price")
	number := c.PostForm("number")
	content := c.PostForm("content")

	file, errFile := c.FormFile("imgPath")
	// 获取文件名称及路径 ./static/upload/goods/xxx.jpg
	imgPath := ""
	imgTag := false
	if file != nil {
		if errFile == nil {
			imgPath = file.Filename
			imgTag = true
			dst := path.Join("./static/upload/goods", imgPath)
			c.SaveUploadedFile(file, dst)
		} else {
			c.Redirect(http.StatusFound, "/back/goodsMgr/curPage/1?res=-1")
			return
		}
	}
	fmt.Println("goods=======:", id, title, imgPath)

	idNum, _ := strconv.Atoi(id)
	categoryNum, _ := strconv.Atoi(category)
	priceN64, _ := strconv.ParseFloat(price, 64)
	priceNum := float32(priceN64)
	numberNum, _ := strconv.Atoi(number)
	if idNum == 0 {
		dbObj := models.Goods{
			Title:      title,
			Category:   categoryNum,
			Price:      priceNum,
			Number:     numberNum,
			Content:    content,
			ImgPath:    imgPath,
			CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			UpdateTime: time.Now().Format("2006-01-02 15:04:05"),
		}
		models.DB.Create(&dbObj)
		c.Redirect(http.StatusFound, "/back/goodsMgr/curPage/1?res=1")
	} else if idNum > 0 {
		dbObj := models.Goods{Id: idNum}
		models.DB.Find(&dbObj)
		dbObj.Title = title
		dbObj.Category = categoryNum
		dbObj.Price = priceNum
		dbObj.Number = numberNum
		dbObj.Content = content
		if imgTag {
			dbObj.ImgPath = imgPath
		}
		dbObj.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
		models.DB.Save(&dbObj)
		c.Redirect(http.StatusFound, "/back/goodsMgr/curPage/1?res=1")
	} else {
		c.Redirect(http.StatusFound, "/back/goodsMgr/curPage/1?res=-1")
	}
}
func (con GoodsController) AddGoods(c *gin.Context) {
	// 验证用户是否过期
	_, err := c.Cookie("id")
	if err != nil {
		c.Redirect(http.StatusFound, "/back/login?error=3")
		return
	}
	c.HTML(http.StatusOK, "backstage/goodsInfo.html", gin.H{
		"optKey": 1,
		"info":   models.Goods{},
	})
}
func (con GoodsController) EditGoods(c *gin.Context) {
	// 验证用户是否过期
	_, err := c.Cookie("id")
	if err != nil {
		c.Redirect(http.StatusFound, "/back/login?error=3")
		return
	}
	id := c.Param("id")
	dbObj := models.Goods{}
	models.DB.Where("id=?", id).Find(&dbObj)
	c.HTML(http.StatusOK, "backstage/goodsInfo.html", gin.H{
		"optKey": 2,
		"info":   dbObj,
	})
}
func (con GoodsController) DeleteGoods(c *gin.Context) {
	// 验证用户是否过期
	_, err := c.Cookie("id")
	if err != nil {
		c.Redirect(http.StatusFound, "/back/login?error=3")
		return
	}
	id := c.Param("id")
	idNum, _ := strconv.Atoi(id)
	dbObj := models.Goods{}
	models.DB.Where("id=?", idNum).Find(&dbObj)
	// 从本地服务器删除
	fileName := dbObj.ImgPath
	if fileName != "" {
		allPath := "./static/upload/goods/" + fileName
		os.Remove(allPath)

		// errDF := os.Remove(allPath)
		// if errDF != nil {
		// 	c.Redirect(http.StatusFound, "/back/goodsMgr/curPage/1?res=-1")
		// 	return
		// }
	}
	// 从数据库中删除
	delObj := models.Goods{Id: idNum}
	models.DB.Delete(&delObj)
	c.Redirect(http.StatusFound, "/back/goodsMgr/curPage/1?res=1")
}
func (con GoodsController) DetailGoods(c *gin.Context) {
	// 验证用户是否过期
	_, err := c.Cookie("id")
	if err != nil {
		c.Redirect(http.StatusFound, "/back/login?error=3")
		return
	}
	id := c.Param("id")
	dbObj := models.Goods{}
	models.DB.Where("id=?", id).Find(&dbObj)
	c.HTML(http.StatusOK, "backstage/goodsInfo.html", gin.H{
		"optKey": 3,
		"info":   dbObj,
	})
}
